'use strict'

function Update(id) {
    window.location.href = `./addOrEdit.html?id=${id}`;
}

function Delete(id) {
    $.ajax({
        url: `http://localhost:8080/product/${id}`,
        type: 'DELETE',
        success: function (res) {
            let tb = $('table');
            tb.html(`
                <tr>
                    <input type="text" id="text" placeholder="请输入查找内容">
                    <input type="button" value="搜索" onclick="Query()">
                    <input type="button" value="添加" onclick="Insert()">
                </tr>
                <tr>
                    <th>ID</th>
                    <th>ProductName</th>
                    <th>Price</th>
                    <th>操作</th>
                </tr>
            `)
            res.forEach(item => {
                tb.append(`
                    <tr>
                        <td>${item.id}</td>
                        <td>${item.productname}</td>
                        <td>${item.price}</td>
                        <td>
                            <input type="button" value="修改" onclick="Update(${item.id})">
                            <input type="button" value="删除" onclick="Delete(${item.id})">
                        </td>
                    </tr>
                `)
            });
            alert('删除成功！')
        }
    })
}

function Save() {
    let id = window.location.search.split('=')[1] || '';
    let obj = {
        productname: $('[id = name]').val(),
        price: $('[id = price]').val(),
    }
    console.log(obj);
    if (id) {
        $.ajax({
            url: `http://localhost:8080/product/${id}`,
            type: 'PUT',
            data: obj,
            success: function (res) {
                alert('保存成功！');
                window.location.href = './index.html'
            }
        })
    } else {
        $.post('http://localhost:8080/product', obj, function (res) {
            alert('保存成功！');
            window.location.href = './index.html'
        })
    }
}

function Back() {
    window.location.href = './index.html'
}

function Query() {
    let keyword = $('[id = text]').val();
    $.get(`http://localhost:8080/product?keyword=${keyword}`, function (res) {
        let tb = $('table');
        tb.html(`
            <tr>
                <input type="text" id="text" placeholder="请输入查找内容">
                <input type="button" value="搜索" onclick="Query()">
                <input type="button" value="添加" onclick="Insert()">
            </tr>
            <tr>
                <th>ID</th>
                <th>ProductName</th>
                <th>Price</th>
                <th>操作</th>
            </tr>
        `)
        res.forEach(item => {
            tb.append(`
                <tr>
                    <td>${item.id}</td>
                    <td>${item.productname}</td>
                    <td>${item.price}</td>
                    <td>
                        <input type="button" value="修改" onclick="Update(${item.id})">
                        <input type="button" value="删除" onclick="Delete(${item.id})">
                    </td>
                </tr>
            `)
        });
    })
}
function Insert() {
    window.location.href = `./addOrEdit.html`;
}